import { ROAST_CONFIG } from "../config";

export default {
    //列表
    getCafes:function (page) {
        if(!page){
            page=1;
        }
        return axios.get(ROAST_CONFIG.API_URL+'/cafes',{
            params:{
                page:page
            }
        });
    },
    //详情
    getCafe:function (id) {
        return axios.get(ROAST_CONFIG.API_URL+'/cafes/'+id);
    },

    // createCafe:function (name,address,city,state,zip) {
    //     return axios.post(ROAST_CONFIG.API_URL+'/cafes',{
    //         name:name,
    //         address:address,
    //         city:city,
    //         state:state,
    //         zip:zip
    //     })
    // },
    //创建
    createCafe:function (name,website,description,roaster,locations,photos) {
        let formData = new FormData();
        formData.append('name',name);
        formData.append('website',website);
        formData.append('description',description);
        formData.append('roaster',roaster);
        //formData.append('locations',locations);
        formData.append('photos',photos);

        locations.forEach(function (item,index) {
            formData.append('locations['+index+'][address]',item.address);
            formData.append('locations['+index+'][city]',item.city);
            formData.append('locations['+index+'][state]',item.state);
            formData.append('locations['+index+'][zip]',item.zip);
            formData.append('locations['+index+'][name]',item.name);
            if(item.methodsAvailable.length>0){
                item.methodsAvailable.forEach(function (method,methodIndex) {
                    formData.append('locations['+index+'][methodsAvailable]['+methodIndex+']',method);
                })
            }
            if(item.tags.length>0){
                item.tags.forEach(function (tag,tagIndex) {
                    formData.append('locations['+index+'][tags]['+tagIndex+']',tag);
                })
            }
        });
        return axios.post(ROAST_CONFIG.API_URL+'/cafes',formData,{
            headers:{
                'Content-Type': 'multipart/form-data'
            }
        })
    },
    //喜欢
    likeCafe:function (cafeId) {
        return axios.post(ROAST_CONFIG.API_URL+'/cafes/'+cafeId+'/like');
    },
    //取消喜欢
    unlikeCafe:function (cafeId) {
        return axios.delete(ROAST_CONFIG.API_URL+'/cafes/'+cafeId+'/like');
    }
}
